package student;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Scanner;

public class TCPClient {
	
	public static void main(String[] args) {
		try {
			Socket client = new Socket("192.168.31.10", 1001);
			System.out.println("链接上了服务器");

			Scanner sc=new Scanner(System.in);

			String line=null;
			OutputStream out=client.getOutputStream();
			DataOutputStream dout=new DataOutputStream(out);
			while (!"exit".equalsIgnoreCase((line=sc.nextLine()))){
				System.out.println("向服务器端发送消息"+line);
				dout.writeUTF(line);
				dout.flush();
			}

//			OutputStream out = client.getOutputStream();
//			out.write(new byte[] {1,2,3,4,5});
//			out.flush();
//			System.out.println("发送了一段字节数组");

			InputStream in=client.getInputStream();
			byte[] bs=new byte[15];
			int len = in.read(bs);
			System.out.println("接收一段客户端的字节数组：" + len + "=" + Arrays.toString(bs));
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}